package com.hairline.service;

import com.hairline.bean.Keyword;
import com.hairline.bean.KeywordExample;
import com.hairline.bean.SearchHistory;
import com.hairline.bean.SearchHistoryExample;
import com.hairline.bean.vo.WxSearchIndexVo;
import com.hairline.bean.vo.WxSearchKeywordHistoryVo;
import com.hairline.mapper.KeywordMapper;
import com.hairline.mapper.SearchHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fenglin on 2020/9/4 5:05 PM
 */
@Service
public class WxSearchServiceImpl implements WxSearchService {

    @Autowired
    KeywordMapper keywordMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    /**
     * 显示分类中的搜索界面
     * @return
     */
    @Override
    public WxSearchIndexVo searchIndex() {
        //获取defaultKeyword
        KeywordExample defaultKeywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = defaultKeywordExample.createCriteria();
        criteria.andIsDefaultEqualTo(true);
        criteria.andDeletedEqualTo(false);
        List<Keyword> defaultKeywordList = keywordMapper.selectByExample(defaultKeywordExample);

        //获取hotKeywordList
        KeywordExample hotKeywordListExample = new KeywordExample();
        KeywordExample.Criteria criteria1 = hotKeywordListExample.createCriteria();
        criteria1.andIsHotEqualTo(true);
        criteria1.andDeletedEqualTo(false);
        List<Keyword> hotKeywordList = keywordMapper.selectByExample(hotKeywordListExample);

        //获取historyKeywordList;
        SearchHistoryExample example = new SearchHistoryExample();
        List<WxSearchKeywordHistoryVo> historyKeywordList = searchHistoryMapper.selectHistoryKeywordListByExample(example);

        //返回
        WxSearchIndexVo wxSearchIndexVo = new WxSearchIndexVo(defaultKeywordList.get(0),hotKeywordList, historyKeywordList);
        return wxSearchIndexVo;
    }

    /**
     * 显示相关历史搜索记录
     * @param keyword 用户输入的内容
     * @return
     */
    @Override
    public List<String> searchHelper(String keyword) {
        SearchHistoryExample example = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = example.createCriteria();
        criteria.andKeywordLike("%" + keyword + "%");
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(example);
        //取出keywordList
        List<String> keywordList = new ArrayList<>();
        for(SearchHistory searchHistory: searchHistories){
            keywordList.add(searchHistory.getKeyword());
        }
        return keywordList;
    }


    /**
     * 清除搜索历史记录--物理删除
     * @return
     */
    @Override
    public int clearhistory() {
        SearchHistoryExample example = new SearchHistoryExample();
        return searchHistoryMapper.deleteByExample(example);
    }
}
